/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogToevoegen.java
 *
 * Created on 19-feb-2012, 16:38:09
 */

package my.StudentAdministratie;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;

import my.StudentAdministratie.XMLWrapper.XMLException;


public class DialogToevoegen extends javax.swing.JFrame {
	private static final long serialVersionUID = -6764356942423297214L;
	public static final String DATE_FORMAT_NOW = "dd-MM-yyyy";

	private XMLWrapper xmlWrapper;

	private String naam;
	private String studentnummer;
	private String woonplaats;
	private String adres;
	private String postcode;
	private String telefoonnr;
	private String geslacht;
	private String inschrijfDatum;

	/** Creates new form DialogToevoegen */
	public DialogToevoegen(XMLWrapper xmlWrapper) throws ParserConfigurationException {
		this.xmlWrapper = xmlWrapper;

		initComponents();
	}

	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		buToevoegen = new javax.swing.JButton();
		tfAdres = new javax.swing.JTextField();
		tfPostcode = new javax.swing.JTextField();
		tfWoonplaats = new javax.swing.JTextField();
		jLabel3 = new javax.swing.JLabel();
		jLabel7 = new javax.swing.JLabel();
		tfStudentNummer = new javax.swing.JTextField();
		jLabel6 = new javax.swing.JLabel();
		tfNaam = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jLabel9 = new javax.swing.JLabel();
		tfTelefoonNummer = new javax.swing.JTextField();
		RadioFemale = new javax.swing.JRadioButton();
		RadioMale = new javax.swing.JRadioButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jLabel1.setText("Naam");

		jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18));
		jLabel4.setText("Toevoegen student");

		buToevoegen.setText("Toevoegen");
		buToevoegen.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buToevoegenActionPerformed(evt);
			}
		});

		jLabel3.setText("Woonplaats");

		jLabel7.setText("Postcode");

		jLabel6.setText("Adres");

		jLabel2.setText("Studentnummer");

		jLabel5.setText("Telefoonnummer");

		jLabel9.setText("Geslacht");

		RadioFemale.setText("V");

		RadioMale.setText("M");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
								.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
										.addContainerGap()
										.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
												.addGap(40, 40, 40)
												.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jLabel3)
														.addComponent(jLabel6)
														.addComponent(jLabel7)
														.addComponent(jLabel5)
														.addComponent(jLabel2)
														.addComponent(jLabel1)
														.addComponent(jLabel9))
														.addGap(109, 109, 109)
														.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
																.addComponent(tfNaam, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
																.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
																		.addComponent(RadioMale)
																		.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 97, Short.MAX_VALUE)
																		.addComponent(RadioFemale))
																		.addComponent(tfStudentNummer, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
																		.addComponent(tfWoonplaats, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
																		.addComponent(tfPostcode, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
																		.addComponent(tfAdres, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
																		.addComponent(buToevoegen, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
																		.addComponent(tfTelefoonNummer, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))))
																		.addGap(74, 74, 74))
				);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jLabel4)
						.addGap(37, 37, 37)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(jLabel1)
								.addComponent(tfNaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(17, 17, 17)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(tfStudentNummer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(jLabel2))
										.addGap(18, 18, 18)
										.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel3)
												.addComponent(tfWoonplaats, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
												.addGap(18, 18, 18)
												.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel6)
														.addComponent(tfAdres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
														.addGap(18, 18, 18)
														.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
																.addComponent(jLabel7)
																.addComponent(tfPostcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
																.addGap(18, 18, 18)
																.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
																		.addComponent(jLabel5)
																		.addGroup(layout.createSequentialGroup()
																				.addComponent(tfTelefoonNummer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addGap(18, 18, 18)
																				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(RadioFemale)
																						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
																								.addComponent(RadioMale)
																								.addComponent(jLabel9)))))
																								.addGap(18, 18, 18)
																								.addComponent(buToevoegen)
																								.addContainerGap(23, Short.MAX_VALUE))
				);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void buToevoegenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

		naam = tfNaam.getText();
		studentnummer = tfStudentNummer.getText();
		woonplaats = tfWoonplaats.getText();
		adres = tfAdres.getText();
		postcode = tfPostcode.getText();
		telefoonnr = tfTelefoonNummer.getText();


		if (RadioMale.isSelected())
			geslacht = "M";
		if(RadioFemale.isSelected())
			geslacht = "V";


		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
		inschrijfDatum = sdf.format(cal.getTime());

		if(studentnummer.equals("") || naam.equals("")) {
			JOptionPane.showMessageDialog(this, "Vul aub minimaal een naam en student nummer in!");
		} else {
			try {
				xmlWrapper.writeStudent(new Student(studentnummer, naam, adres, postcode, woonplaats, geslacht, telefoonnr, inschrijfDatum), false);
				this.dispose();
			} catch(XMLException e) {
				JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
			}
		}

	}//GEN-LAST:event_jButton3ActionPerformed

	//    public static void main(String args[]) {
	//        java.awt.EventQueue.invokeLater(new Runnable() {
	//            public void run() {
	//                try {
	//                    new DialogToevoegen().setVisible(true);
	//                } catch (ParserConfigurationException ex) {
	//                    Logger.getLogger(DialogToevoegen.class.getName()).log(Level.SEVERE, null, ex);
	//                }
	//            }
	//        });
	//    }

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JRadioButton RadioFemale;
	private javax.swing.JRadioButton RadioMale;
	private javax.swing.JButton buToevoegen;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel9;
	private javax.swing.JTextField tfNaam;
	private javax.swing.JTextField tfStudentNummer;
	private javax.swing.JTextField tfWoonplaats;
	private javax.swing.JTextField tfAdres;
	private javax.swing.JTextField tfPostcode;
	private javax.swing.JTextField tfTelefoonNummer;
	// End of variables declaration//GEN-END:variables

}
